package com.quanyan.ic.vendingMachine.mapper.customSQL;

import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhuo.shi on 2017/8/3.
 */
public class VmInventoryMapperSqlProvider {

    public static final Logger logger = LoggerFactory.getLogger(VmInventoryMapperSqlProvider.class);

    public String updateGridInventory(final String vmNumber, final String grid, final Byte gridNumber, final Integer opCount) {
        SQL sql = new SQL() {
            {
                UPDATE("vm_grid_goods_mapping");
                SET("inventory = inventory + " + opCount);
                WHERE("vm_number='" + vmNumber + "'");
                AND();
                WHERE("grid='" + grid + "'");
                AND();
                WHERE("grid_number=" + gridNumber);
                AND();
                WHERE("inventory + " + opCount + " >= 0");
            }
        };
        logger.info("updateGridInventory SQL: \n{}", sql.toString());
        return sql.toString();
    }

    public String countVmInventoryOfGoods(final String vmNumber, final Integer goodsId, final String goodsUuid) {
        SQL sql = new SQL() {
            {
                SELECT("SUM(inventory)");
                FROM("vm_grid_goods_mapping");
                WHERE("vm_number='" + vmNumber + "'");
                AND();
                WHERE("goods_id=" + goodsId);
                AND();
                WHERE("goods_uuid='" + goodsUuid + "'");
            }
        };
        logger.info("countVmInventoryOfGoods SQL: \n{}", sql.toString());
        return sql.toString();
    }
}
